package com.sihan.framework.etm.common;

import com.sihan.framework.etm.interceptor.AuthHandlerInterceptor;
import com.sihan.framework.etm.service.OperatorService;
import com.sihan.framework.etm.service.SecurityParamService;
import com.sihan.framework.etm.service.UserService;

import org.apache.catalina.connector.Connector;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @author QinC
 * @create 2018-09-12 9:25
 */
@Configuration
@ConditionalOnClass(ShiroFilterFactoryBean.class)
public class MyMvcConfig extends WebMvcConfigurerAdapter {

    /**
     * http端口
     */
    @Value("${server.http-port:8080}")
    private Integer httpPort;
    /**
     * https端口
     */
    @Value("${server.port:8443}")
    private Integer httpsPort;

    @Autowired
    private UserService userService;

    @Autowired
    private SecurityParamService securityParamService;

    @Autowired
    private OperatorService operatorService;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/home/tomcat/upload/files/basis/**")
                .addResourceLocations("file:/home/tomcat/upload/files/basis/");
        registry.addResourceHandler("/home/tomcat/upload/files/contract/**")
                .addResourceLocations("file:/home/tomcat/upload/files/contract/");
        registry.addResourceHandler("/home/tomcat/upload/files/file/**")
                .addResourceLocations("file:/home/tomcat/upload/files/file/");
        registry.addResourceHandler("/home/tomcat/upload/files/workguide/**")
                .addResourceLocations("file:/home/tomcat/upload/files/workguide/");
        registry.addResourceHandler("/home/tomcat/upload/files/comfirm/**")
                .addResourceLocations("file:/home/tomcat/upload/files/comfirm/");
        registry.addResourceHandler("/home/tomcat/upload/files/report/**")
                .addResourceLocations("file:/home/tomcat/upload/files/report/");
        registry.addResourceHandler("/home/tomcat/upload/files/bid/**")
                .addResourceLocations("file:/home/tomcat/upload/files/bid/");
        registry.addResourceHandler("/home/tomcat/upload/files/template/**")
                .addResourceLocations("file:/home/tomcat/upload/files/template/");
        registry.addResourceHandler("/home/tomcat/upload/files/inspected/**")
                .addResourceLocations("file:/home/tomcat/upload/files/inspected/");
        registry.addResourceHandler("/home/tomcat/upload/image/**")
                .addResourceLocations("file:/home/tomcat/upload/image/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    @Bean
    public AuthHandlerInterceptor authHandlerInterceptor() {
        AuthHandlerInterceptor interceptor = new AuthHandlerInterceptor();
        interceptor.setUserService(userService);
        interceptor.setSecurityParamService(securityParamService);
        interceptor.setOperatorService(operatorService);
        return interceptor;
    }

    //    @Bean
//    public EmbeddedServletContainerFactory servletContainer(){
//        TomcatEmbeddedServletContainerFactory tomcat=new TomcatEmbeddedServletContainerFactory(){
//            @Override
//            protected void postProcessContext(Context context) {
//                SecurityConstraint securityConstraint=new SecurityConstraint();
//                securityConstraint.setUserConstraint("CONFIDENTIAL");//confidential
//                SecurityCollection collection=new SecurityCollection();
//                collection.addPattern("/*");
//                securityConstraint.addCollection(collection);
//                context.addConstraint(securityConstraint);
//            }
//        };
//        tomcat.addAdditionalTomcatConnectors(httpConnector());
//        return tomcat;
//    }
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                if (container instanceof TomcatEmbeddedServletContainerFactory) {
                    TomcatEmbeddedServletContainerFactory containerFactory =
                            (TomcatEmbeddedServletContainerFactory) container;

                    Connector connector = new Connector(TomcatEmbeddedServletContainerFactory.DEFAULT_PROTOCOL);
                    connector.setPort(httpPort);
                    containerFactory.addAdditionalTomcatConnectors(connector);
                }
            }
        };
    }

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(httpPort);
        connector.setSecure(false);
        connector.setRedirectPort(httpsPort);
        return connector;
    }
}
